﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace Gap.Bll.BritishGas
{
    [Table(Name = "[BritishGas].[BritishGasCC]")]
    public class CarbonCalculation
    {
        public CarbonCalculation()
        {
        }

        public static CarbonCalculation CreateInstance(string schoolId, string className, DateTime start, DateTime end)
        {
            CarbonCalculation tmp = new CarbonCalculation();
            tmp._SchoolId = schoolId;
            tmp._ClassName = className;
            tmp._Start = new DateTime(start.Year, start.Month, start.Day);
            tmp._End = new DateTime(end.Year, end.Month, end.Day);

            return tmp;
        }

        public static CarbonCalculation Get(string schoolId, string className, DateTime start, DateTime end)
        {
            BllContext context = new BllContext();

            DateTime tmpStart = new DateTime(start.Year, start.Month, start.Day);
            DateTime tmpEnd = new DateTime(end.Year, end.Month, end.Day);

            var query = from t in context.CarbonCalculation
                        where t.SchoolId == schoolId && t.ClassName == className && t.Start == tmpStart && t.End == tmpEnd
                        select t;
            if (query.Count() == 1)
            {
                CarbonCalculation tmp = query.Single();
                return tmp;
            }
            else
                return null;
        }

        private int _Id;
        [Column(Storage = "_Id", DbType = "Int", CanBeNull = false, IsDbGenerated = true)]
        public int Id
        {
            get { return _Id; }
        }

        private string _SchoolId;
        [Column(Storage="_SchoolId", DbType="VarChar(50)", IsPrimaryKey=true, CanBeNull=false)]
        public string SchoolId
        {
            get { return _SchoolId; }
        }

        private string _ClassName;
        [Column(Storage="_ClassName", DbType="VarChar(50)", IsPrimaryKey=true, CanBeNull=false)]
        public string ClassName
        {
            get { return _ClassName; }
        }

        private DateTime _Start;
        [Column(Storage = "_Start", DbType = "DateTime", IsPrimaryKey = true, CanBeNull = false)]
        public DateTime Start
        {
            get { return _Start; }
        }

        private DateTime _End;
        [Column(Storage = "_End", DbType = "DateTime", IsPrimaryKey = true, CanBeNull = false)]
        public DateTime End
        {
            get { return _End; }
        }


        private int _NoOfPupils;
        [Column(Storage="_NoOfPupils", DbType="Int", CanBeNull=false)]
        public int NoOfPupils
        {
            get { return _NoOfPupils; }
            set { _NoOfPupils = value; }
        }

        private float? _ElectricityKwh;
        [Column(Storage = "_ElectricityKwh", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? ElectricityKwh
        {
            get
            {
                return _ElectricityKwh;
            }
            set
            {
                _ElectricityKwh = value;
            }
        }

        private float? _GasKwh;
        [Column(Storage = "_GasKwh", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? GasKwh
        {
            get { return _GasKwh; }
            set { _GasKwh = value; }
        }

        private float? _OilKwh;
        [Column(Storage = "_OilKwh", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? OilKwh
        {
            get { return _OilKwh; }
            set { _OilKwh = value; }
        }


        private float? _OilLitres;
        [Column(Storage = "_OilLitres", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? OilLitres
        {
            get { return _OilLitres; }
            set { _OilLitres = value; }
        }


        private float? _Butane45;
        [Column(Storage = "_Butane45", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Butane45
        {
            get { return _Butane45; }
            set { _Butane45 = value; }
        }

        private float? _Butane70;
        [Column(Storage = "_Butane70", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Butane70
        {
            get { return _Butane70; }
            set { _Butane70 = value; }
        }

        private float? _Butane150;
        [Column(Storage = "_Butane150", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Butane150
        {
            get { return _Butane150; }
            set { _Butane150 = value; }
        }

        private float? _Propane39;
        [Column(Storage = "_Propane39", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Propane39
        {
            get { return _Propane39; }
            set { _Propane39 = value; }
        }

        private float? _Propane60;
        [Column(Storage = "_Propane60", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Propane60
        {
            get { return _Propane60; }
            set { _Propane60 = value; }
        }

        private float? _Propane130;
        [Column(Storage = "_Propane130", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? Propane130
        {
            get { return _Propane130; }
            set { _Propane130 = value; }
        }

        private float? _RenewablePercentage;
        [Column(Storage = "_RenewablePercentage", DbType = "Decimal(4,3)", CanBeNull = true)]
        public float? RenewablePercentage
        {
            get { return _RenewablePercentage; }
            set { _RenewablePercentage = value; }
        }

        private float? _GreenElectricity;
        [Column(Storage = "_GreenElectricity", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? GreenElectricity
        {
            get { return _GreenElectricity; }
            set { _GreenElectricity = value; }
        }

        private float? _SolarPanels;
        [Column(Storage = "_SolarPanels", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? SolarPanels
        {
            get { return _SolarPanels; }
            set { _SolarPanels = value; }
        }

        private float? _WindTurbine;
        [Column(Storage = "_WindTurbine", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? WindTurbine
        {
            get { return _WindTurbine; }
            set { _WindTurbine = value; }
        }

        private float? _RenewableOther;
        [Column(Storage = "_RenewableOther", DbType = "Decimal(10,4)", CanBeNull = true)]
        public float? RenewableOther
        {
            get { return _RenewableOther; }
            set { _RenewableOther = value; }
        }

        private object _ConcurencyId;
        [Column(Storage = "_ConcurencyId", DbType = "RowVersion", IsVersion = true, IsDbGenerated = true)]
        public object ConcurencyId
        {
            get { return _ConcurencyId; }
        }

        public void SaveChanges()
        {
            Bll.BllContext context = new BllContext();
            if (this.Id == 0)
                context.CarbonCalculation.InsertOnSubmit(this);
            else
                context.CarbonCalculation.Attach(this, true);
            context.SubmitChanges();
        }

        public void CalculateFootprint()
        {
            float tmpElec = 0;
            if(ElectricityKwh != null)
                tmpElec = ElectricityKwh.Value * 0.523f;

            _ElectricityTotal = tmpElec * (1 - (RenewablePercentage == null ? 0 : RenewablePercentage.Value));

            _GasTotal = 0;
            if (GasKwh != null)
                _GasTotal = GasKwh.Value * 0.185f;

            float tmpOilKWH = 0;
            float tmpOilLtr = 0;
            if (OilKwh != null)
                tmpOilKWH = OilKwh.Value * 0.267f;
            if(OilLitres != null)
                tmpOilLtr = OilLitres.Value * 11.7f * 0.267f;

            _OilKwhTotal = tmpOilKWH + tmpOilLtr;

            float tmpB45 = 0;
            float tmpB70 = 0;
            float tmpB150 = 0;

            if(Butane45 != null)
                tmpB45 = Butane45.Value * 4.5f * 13.62f * 0.206f;
            if(Butane70 != null)
                tmpB70 = Butane70.Value * 7f * 13.62f * 0.206f;
            if(Butane150 != null)
                tmpB150 = Butane150.Value * 15f * 13.62f * 0.206f;
            _ButaneTotal = tmpB45 + tmpB70 + tmpB150;

            float tmpP39 = 0;
            float tmpP60 = 0;
            float tmpP130 = 0;

            if(Propane39 != null)
                tmpP39 = Propane39.Value * 3.9f * 13.83f * 0.206f;
            if(Propane60 != null)
                tmpP60 = Propane60.Value * 6f * 13.83f * 0.206f;
            if(Propane130 != null)
                tmpP130 = Propane130.Value * 13f * 13.83f * 0.206f;
            _PropaneTotal = tmpP39 + tmpP60 + tmpP130;

            _TonnesTotal = ElectricityTotal + GasTotal + OilKwhTotal + ButaneTotal + PropaneTotal;

            if (NoOfPupils > 0)
            {
                _ElectricityPerPupil = ElectricityTotal / NoOfPupils;
                _GasPerPupil = GasTotal / NoOfPupils;
                _OilKwhPerPupil = OilKwhTotal / NoOfPupils;
                _ButanePerPupil = ButaneTotal / NoOfPupils;
                _PropanePerPupil = PropaneTotal / NoOfPupils;
                _TonnesPerPupil = TonnesTotal / NoOfPupils;
            }
            else
            {
                _ElectricityPerPupil = 0;
                _GasPerPupil = 0;
                _OilKwhPerPupil = 0;
                _ButanePerPupil = 0;
                _PropanePerPupil = 0;
                _TonnesPerPupil = 0;
            }
        }

        private float _TonnesTotal;
        [Column(Storage = "_TonnesTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float TonnesTotal
        {
            get { return _TonnesTotal; }
        }

        private float _TonnesPerPupil;
        [Column(Storage = "_TonnesPerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float TonnesPerPupil
        {
            get { return _TonnesPerPupil; }
        }

        private float _ElectricityTotal;
        [Column(Storage = "_ElectricityTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float ElectricityTotal
        {
            get { return _ElectricityTotal; }
        }

        private float _ElectricityPerPupil;
        [Column(Storage = "_ElectricityPerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float ElectricityPerPupil
        {
            get { return _ElectricityPerPupil; }
        }

        private float _GasTotal;
        [Column(Storage = "_GasTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float GasTotal
        {
            get { return _GasTotal; }
        }

        private float _GasPerPupil;
        [Column(Storage = "_GasPerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float GasPerPupil
        {
            get { return _GasPerPupil; }
        }

        private float _OilKwhTotal;
        [Column(Storage = "_OilKwhTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float OilKwhTotal
        {
            get { return _OilKwhTotal; }
        }

        private float _OilKwhPerPupil;
        [Column(Storage = "_OilKwhPerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float OilKwhPerPupil
        {
            get { return _OilKwhPerPupil; }
        }

        private float _ButaneTotal;
        [Column(Storage = "_ButaneTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float ButaneTotal
        {
            get { return _ButaneTotal; }
        }

        private float _ButanePerPupil;
        [Column(Storage = "_ButanePerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float ButanePerPupil
        {
            get { return _ButanePerPupil; }
        }

        private float _PropaneTotal;
        [Column(Storage = "_PropaneTotal", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float PropaneTotal
        {
            get { return _PropaneTotal; }
        }

        private float _PropanePerPupil;
        [Column(Storage = "_PropanePerPupil", DbType = "Decimal(10,4)", CanBeNull = false)]
        public float PropanePerPupil
        {
            get { return _PropanePerPupil; }
        }


        public void Delete()
        {
            Bll.BllContext context = new BllContext();
            context.CarbonCalculation.Attach(this);
            context.CarbonCalculation.DeleteOnSubmit(this);
            context.SubmitChanges();
        }
    }
}
